package com.ymatou.iapi.liveseller.testcase;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.liveseller.parameter.AutoAddAuditBean;
import com.ymatou.iapi.liveseller.parameter.CreateSellerLiveBean;
import com.ymatou.iapi.liveseller.service.AutoAddAuditCall;
import com.ymatou.mq.admin.parameter.MessageListBean;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.LiveRoomCallService;
import com.ymttest.business.service.MqQueryCallService;
import com.ymttest.database.model.LiveRoom;
import com.ymttest.database.sqlwapper.LiveRoomWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.YMTDateUtil;
/**
*直播自动添加进推荐池
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_AutoAddAudit{
	private static AutoAddAuditBean autoaddauditBean;
	private static AutoAddAuditCall autoaddauditCall;
	private static int sellerId = Integer.parseInt(EnvSetup.getData("autotest_seller1"));
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("直播自动添加进推荐池");
	}

	@Before
	public void caseUp() {
		autoaddauditBean=new AutoAddAuditBean();
		autoaddauditCall=new AutoAddAuditCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(autoaddauditCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_AutoAddAudit_001() {
		Logger.start(true,"直播自动添加进推荐池成功");
		try {
			Logger.comment("创建直播");
			// 5分钟后开始200分钟后结束
			CreateSellerLiveBean clive = LiveRoomCallService.initCreatesellerliveBean(sellerId);
			Date now = YMTDateUtil.getDate();
			//半小时之内会锁定不能修改
			Date starttime = new Date(now.getTime() + 60 * 60 * 1000);
			Date endtime = new Date(now.getTime() + 200 * 60 * 1000);
			clive.getLiveDto().setStartTime(YMTDateUtil.getFormatTime(starttime, YMTDateUtil.YMDHMS));
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(clive);
			autoaddauditBean.setAppId("Test");
			autoaddauditCall.setData(autoaddauditBean);
			autoaddauditCall.callService();			
			Logger.verifyEquals("ok", autoaddauditCall.getReturnData(), "验证返回");
			Thread.sleep(3000);
			MessageListBean MessageListBean = MqQueryCallService.initMessageListBean("livevideo", "auto_audit", createlive.getId().toString());
			 List<Map> joa = MqQueryCallService.GetMqMessage(MessageListBean);
			if(joa.size()==0){
				Thread.sleep(5000);
				 joa = MqQueryCallService.GetMqMessage(MessageListBean);
			}
			joa.forEach(ele->{
				Logger.debug(ele.toString());
			});
			Logger.verifyEquals(1, joa.size(), "发送消息成功");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_AutoAddAudit_002() {
		Logger.start(true,"status已开始不发送");
		try {
			Logger.comment("创建直播");
			// 5分钟后开始200分钟后结束
			CreateSellerLiveBean clive = LiveRoomCallService.initCreatesellerliveBean(sellerId);
			Date now = YMTDateUtil.getDate();
			//半小时之内会锁定不能修改
			Date starttime = new Date(now.getTime() + 60 * 60 * 1000);
			Date endtime = new Date(now.getTime() + 200 * 60 * 1000);
			clive.getLiveDto().setStartTime(YMTDateUtil.getFormatTime(starttime, YMTDateUtil.YMDHMS));
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(clive);
			LiveRoomCallService.SellerLiveOnline( createlive.getId(), sellerId);
			autoaddauditBean.setAppId("Test");
			autoaddauditCall.setData(autoaddauditBean);
			autoaddauditCall.callService();			
			Logger.verifyEquals("ok", autoaddauditCall.getReturnData(), "验证返回");
			Thread.sleep(3000);
			MessageListBean MessageListBean = MqQueryCallService.initMessageListBean("livevideo", "auto_audit", createlive.getId().toString());
			 List<Map> joa = MqQueryCallService.GetMqMessage(MessageListBean);
			if(joa.size()==0){
				Thread.sleep(5000);
				 joa = MqQueryCallService.GetMqMessage(MessageListBean);
			}
			joa.forEach(ele->{
				Logger.debug(ele.toString());
			});
			Logger.verifyEquals(0, joa.size(), "发送消息成功");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_AutoAddAudit_003() {
		Logger.start(true,"auditStatus已审核不发送");
		try {
			Logger.comment("创建直播");
			// 5分钟后开始200分钟后结束
			CreateSellerLiveBean clive = LiveRoomCallService.initCreatesellerliveBean(sellerId);
			Date now = YMTDateUtil.getDate();
			//半小时之内会锁定不能修改
			Date starttime = new Date(now.getTime() + 60 * 60 * 1000);
			Date endtime = new Date(now.getTime() + 200 * 60 * 1000);
			clive.getLiveDto().setStartTime(YMTDateUtil.getFormatTime(starttime, YMTDateUtil.YMDHMS));
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(clive);
			LiveRoomCallService.AuditLive(createlive.getId(), 1);
			autoaddauditBean.setAppId("Test");
			autoaddauditCall.setData(autoaddauditBean);
			autoaddauditCall.callService();			
			Logger.verifyEquals("ok", autoaddauditCall.getReturnData(), "验证返回");
			Thread.sleep(3000);
			MessageListBean MessageListBean = MqQueryCallService.initMessageListBean("livevideo", "auto_audit", createlive.getId().toString());
			 List<Map> joa = MqQueryCallService.GetMqMessage(MessageListBean);
			if(joa.size()==0){
				Thread.sleep(5000);
				 joa = MqQueryCallService.GetMqMessage(MessageListBean);
			}
			joa.forEach(ele->{
				Logger.debug(ele.toString());
			});
			Logger.verifyEquals(0, joa.size(), "发送消息成功");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_AutoAddAudit_004() {
		Logger.start(true,"直播已经过了开始时间不发送");
		try {
			Logger.comment("创建直播");
			// 5分钟后开始200分钟后结束
			CreateSellerLiveBean clive = LiveRoomCallService.initCreatesellerliveBean(sellerId);
			Date now = YMTDateUtil.getDate();
			//半小时之内会锁定不能修改
			Date starttime = new Date(now.getTime() + 60 * 60 * 1000);
			Date endtime = new Date(now.getTime() + 200 * 60 * 1000);
			clive.getLiveDto().setStartTime(YMTDateUtil.getFormatTime(starttime, YMTDateUtil.YMDHMS));
			
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(clive);
			LiveRoomCallService.UpdateLiveTimeToStart(createlive.getId());
			autoaddauditBean.setAppId("Test");
			autoaddauditCall.setData(autoaddauditBean);
			autoaddauditCall.callService();			
			Logger.verifyEquals("ok", autoaddauditCall.getReturnData(), "验证返回");
			Thread.sleep(3000);
			MessageListBean MessageListBean = MqQueryCallService.initMessageListBean("livevideo", "auto_audit", createlive.getId().toString());
			 List<Map> joa = MqQueryCallService.GetMqMessage(MessageListBean);
			if(joa.size()==0){
				Thread.sleep(5000);
				 joa = MqQueryCallService.GetMqMessage(MessageListBean);
			}
			joa.forEach(ele->{
				Logger.debug(ele.toString());
			});
			Logger.verifyEquals(0, joa.size(), "发送消息成功");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
			
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_AutoAddAudit_005() {
		Logger.start(true,"已删除直播不发送");
		try {
			Logger.comment("创建直播");
			// 5分钟后开始200分钟后结束
			CreateSellerLiveBean clive = LiveRoomCallService.initCreatesellerliveBean(sellerId);
			Date now = YMTDateUtil.getDate();
			//半小时之内会锁定不能修改
			Date starttime = new Date(now.getTime() + 60 * 60 * 1000);
			Date endtime = new Date(now.getTime() + 200 * 60 * 1000);
			clive.getLiveDto().setStartTime(YMTDateUtil.getFormatTime(starttime, YMTDateUtil.YMDHMS));
			
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(clive);
			new LiveRoomWapper().updateLiveRoomAction(createlive.getId()	, -1);
			autoaddauditBean.setAppId("Test");
			autoaddauditCall.setData(autoaddauditBean);
			autoaddauditCall.callService();			
			Logger.verifyEquals("ok", autoaddauditCall.getReturnData(), "验证返回");
			Thread.sleep(3000);
			MessageListBean MessageListBean = MqQueryCallService.initMessageListBean("livevideo", "auto_audit", createlive.getId().toString());
			 List<Map> joa = MqQueryCallService.GetMqMessage(MessageListBean);
			if(joa.size()==0){
				Thread.sleep(5000);
				 joa = MqQueryCallService.GetMqMessage(MessageListBean);
			}
			joa.forEach(ele->{
				Logger.debug(ele.toString());
			});
			Logger.verifyEquals(0, joa.size(), "发送消息成功");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
			
	}
	@Test
	@Category({P1.class})
	@TestCase
//	#直播添加推荐池
//	#忽略消息标题（首页会露出）disconfig配置
//	ignoreAddAuditSellerIds = 3383,512003
	public void Tc_AutoAddAudit_006() {
		Logger.start(true,"直播自动添加进推荐池-ignoreAddAuditSellerIds忽略");
		try {
			Logger.comment("创建直播");
			// 5分钟后开始200分钟后结束
			CreateSellerLiveBean clive = LiveRoomCallService.initCreatesellerliveBean(3383);
			Date now = YMTDateUtil.getDate();
			//半小时之内会锁定不能修改
			Date starttime = new Date(now.getTime() + 60 * 60 * 1000);
			Date endtime = new Date(now.getTime() + 200 * 60 * 1000);
			clive.getLiveDto().setStartTime(YMTDateUtil.getFormatTime(starttime, YMTDateUtil.YMDHMS));
			
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(clive);
			autoaddauditBean.setAppId("Test");
			autoaddauditCall.setData(autoaddauditBean);
			autoaddauditCall.callService();			
			Logger.verifyEquals("ok", autoaddauditCall.getReturnData(), "验证返回");
			Thread.sleep(3000);
			MessageListBean MessageListBean = MqQueryCallService.initMessageListBean("livevideo", "auto_audit", createlive.getId().toString());
			 List<Map> joa = MqQueryCallService.GetMqMessage(MessageListBean);
			if(joa.size()==0){
				Thread.sleep(5000);
				 joa = MqQueryCallService.GetMqMessage(MessageListBean);
			}
			joa.forEach(ele->{
				Logger.debug(ele.toString());
			});
			Logger.verifyEquals(0, joa.size(), " 忽略消息");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}